#ifndef MESSAGELOGPANEL_H
#define MESSAGELOGPANEL_H

#include <QWidget>
#include <QTextBrowser>
#include <QPushButton>
#include <QComboBox>
#include <QCheckBox>
#include <QButtonGroup>
#include <QStackedWidget>
#include <QVBoxLayout>
#include "core/Channel.h"
#include "LogViewerWidget.h"

namespace ModbusPlexLink {

/**
 * @brief Modbus报文查看器
 */
class ModbusMessageViewer : public QWidget {
    Q_OBJECT

public:
    explicit ModbusMessageViewer(QWidget *parent = nullptr);

    // 添加报文
    void addMessage(const QString& direction, const QString& device,
                   const QString& function, const QString& address,
                   const QString& data, bool success = true);

    // 清除报文
    void clearMessages();

    // 设置过滤通道
    void setChannel(Channel* channel);

private slots:
    void onClearClicked();
    void onExportClicked();
    void onPauseToggled(bool paused);

private:
    void setupUi();
    QString formatMessage(const QString& direction, const QString& device,
                         const QString& function, const QString& address,
                         const QString& data, bool success);

    Channel* m_channel;
    QTextBrowser* m_messageDisplay;
    QPushButton* m_clearBtn;
    QPushButton* m_exportBtn;
    QCheckBox* m_pauseCheck;
    bool m_paused;
    int m_messageCount;
};

/**
 * @brief 报文与日志面板 - 侧边切换式布局
 *
 * 功能：
 * - 左侧切换按钮（报文/日志）
 * - 右侧内容区域
 * - 自动筛选当前通道
 */
class MessageLogPanel : public QWidget {
    Q_OBJECT

public:
    explicit MessageLogPanel(QWidget *parent = nullptr);
    ~MessageLogPanel();

    // 设置当前通道
    void setChannel(Channel* channel);

    // 获取子组件
    ModbusMessageViewer* getMessageViewer() { return m_messageViewer; }
    LogViewerWidget* getLogViewer() { return m_logViewer; }

private slots:
    void onMessageModeClicked();
    void onLogModeClicked();

private:
    void setupUi();
    void setActiveButton(QPushButton* button);

    Channel* m_channel;

    // UI组件
    QPushButton* m_messageBtn;
    QPushButton* m_logBtn;
    QStackedWidget* m_stackedWidget;

    ModbusMessageViewer* m_messageViewer;
    LogViewerWidget* m_logViewer;

    QButtonGroup* m_buttonGroup;
};

} // namespace ModbusPlexLink

#endif // MESSAGELOGPANEL_H
